package gitee.derumin.really_forge.mixin;


import gitee.derumin.really_forge.helpers.EnchantmentCache;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.item.Item;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(EnchantmentHelper.class)
public abstract class EnchantmentHelperMixin {

    @Redirect(method = "getPossibleEntries", at = @At(value = "FIELD", target = "Lnet/minecraft/enchantment/Enchantment;target:Lnet/minecraft/enchantment/EnchantmentTarget;", opcode = Opcodes.GETFIELD))
    private static EnchantmentTarget putCache(Enchantment enchantment){
        EnchantmentCache.putCache(enchantment);
        return enchantment.target;
    }

    @Redirect(method = "getPossibleEntries", at = @At(value = "INVOKE", target = "Lnet/minecraft/enchantment/EnchantmentTarget;isAcceptableItem(Lnet/minecraft/item/Item;)Z"))
    private static boolean noCheck(EnchantmentTarget instance, Item item){
        return EnchantmentCache.getCache().isAcceptableItem(item.getDefaultStack());
    }
}
